package com.am.persistent;

import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;

import com.am.cell.ICell;
import com.am.service.ServiceFactory;
import com.am.words.ICharacter;

public class AMMemoryPersistent implements IAMPersistent {

	private Map<ICharacter, ICell> cells;
	private PrintStream ps = System.out;

	public AMMemoryPersistent() {
		cells = new HashMap<ICharacter, ICell>();
	}

	@Override
	public ICell findCellByValue(ICharacter value) {
		if (!cells.containsKey(value)) {
			return null;
		}
		return cells.get(value);
	}

	@Override
	public ICell saveCell(ICell cell) {
		return null;
	}

	@Override
	public ICell createCell(ICharacter value) {
		if (cells.containsKey(value)) {
			return cells.get(value);
		} else {
			ICell cell = ServiceFactory.getInstance().getCellFactory()
					.createCell(value);
			cells.put(value, cell);
			return cell;
		}
	}

	@Override
	public void dumpAllCells() {
		ps.println("Total Cells:" + cells.size());
	}

}
